using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.TraCuuDiem.Components
{
	public partial class TotNghiepCD
	{
		#region Properties.
		
		public string MaSV { set; get; }
		public DateTime Kyluat { set; get; }
		public double GDQP { set; get; }
		public double TTHCM { set; get; }
		public double MonTNCS { set; get; }
		public double MonTNCN { set; get; }
		public double DTNCTK { set; get; }
		public double TLDVHPThiLai { set; get; }
		public string XLTN { set; get; }
		public string HoTenLot { set; get; }
		public string Ten { set; get; }
		public string Lop { set; get; }
		public double GDTC { set; get; }
		public string CongNhanTN { set; get; }
		public string GhiChu { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static TotNghiepCD Load(string maSV)
		{
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, maSV);
			TotNghiepCD entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new TotNghiepCD();
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("Kyluat"))) entity.Kyluat = reader.GetDateTime(reader.GetOrdinal("Kyluat"));
				if (!reader.IsDBNull(reader.GetOrdinal("GDQP"))) entity.GDQP = reader.GetDouble(reader.GetOrdinal("GDQP"));
				if (!reader.IsDBNull(reader.GetOrdinal("TTHCM"))) entity.TTHCM = reader.GetDouble(reader.GetOrdinal("TTHCM"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTNCS"))) entity.MonTNCS = reader.GetDouble(reader.GetOrdinal("MonTNCS"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTNCN"))) entity.MonTNCN = reader.GetDouble(reader.GetOrdinal("MonTNCN"));
				if (!reader.IsDBNull(reader.GetOrdinal("DTNCTK"))) entity.DTNCTK = reader.GetDouble(reader.GetOrdinal("DTNCTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("TLDVHPThiLai"))) entity.TLDVHPThiLai = reader.GetDouble(reader.GetOrdinal("TLDVHPThiLai"));
				if (!reader.IsDBNull(reader.GetOrdinal("XLTN"))) entity.XLTN = reader.GetString(reader.GetOrdinal("XLTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
				if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("GDTC"))) entity.GDTC = reader.GetDouble(reader.GetOrdinal("GDTC"));
				if (!reader.IsDBNull(reader.GetOrdinal("CongNhanTN"))) entity.CongNhanTN = reader.GetString(reader.GetOrdinal("CongNhanTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<TotNghiepCD> SelectCollectionAll()
		{
			List<TotNghiepCD> collection = new List<TotNghiepCD>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll();
			while (reader.Read())
			{
				TotNghiepCD entity = new TotNghiepCD();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("Kyluat"))) entity.Kyluat = reader.GetDateTime(reader.GetOrdinal("Kyluat"));
				if (!reader.IsDBNull(reader.GetOrdinal("GDQP"))) entity.GDQP = reader.GetDouble(reader.GetOrdinal("GDQP"));
				if (!reader.IsDBNull(reader.GetOrdinal("TTHCM"))) entity.TTHCM = reader.GetDouble(reader.GetOrdinal("TTHCM"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTNCS"))) entity.MonTNCS = reader.GetDouble(reader.GetOrdinal("MonTNCS"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTNCN"))) entity.MonTNCN = reader.GetDouble(reader.GetOrdinal("MonTNCN"));
				if (!reader.IsDBNull(reader.GetOrdinal("DTNCTK"))) entity.DTNCTK = reader.GetDouble(reader.GetOrdinal("DTNCTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("TLDVHPThiLai"))) entity.TLDVHPThiLai = reader.GetDouble(reader.GetOrdinal("TLDVHPThiLai"));
				if (!reader.IsDBNull(reader.GetOrdinal("XLTN"))) entity.XLTN = reader.GetString(reader.GetOrdinal("XLTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
				if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("GDTC"))) entity.GDTC = reader.GetDouble(reader.GetOrdinal("GDTC"));
				if (!reader.IsDBNull(reader.GetOrdinal("CongNhanTN"))) entity.CongNhanTN = reader.GetString(reader.GetOrdinal("CongNhanTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<TotNghiepCD> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<TotNghiepCD> collection = new List<TotNghiepCD>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				TotNghiepCD entity = new TotNghiepCD();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("Kyluat"))) entity.Kyluat = reader.GetDateTime(reader.GetOrdinal("Kyluat"));
				if (!reader.IsDBNull(reader.GetOrdinal("GDQP"))) entity.GDQP = reader.GetDouble(reader.GetOrdinal("GDQP"));
				if (!reader.IsDBNull(reader.GetOrdinal("TTHCM"))) entity.TTHCM = reader.GetDouble(reader.GetOrdinal("TTHCM"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTNCS"))) entity.MonTNCS = reader.GetDouble(reader.GetOrdinal("MonTNCS"));
				if (!reader.IsDBNull(reader.GetOrdinal("MonTNCN"))) entity.MonTNCN = reader.GetDouble(reader.GetOrdinal("MonTNCN"));
				if (!reader.IsDBNull(reader.GetOrdinal("DTNCTK"))) entity.DTNCTK = reader.GetDouble(reader.GetOrdinal("DTNCTK"));
				if (!reader.IsDBNull(reader.GetOrdinal("TLDVHPThiLai"))) entity.TLDVHPThiLai = reader.GetDouble(reader.GetOrdinal("TLDVHPThiLai"));
				if (!reader.IsDBNull(reader.GetOrdinal("XLTN"))) entity.XLTN = reader.GetString(reader.GetOrdinal("XLTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
				if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("GDTC"))) entity.GDTC = reader.GetDouble(reader.GetOrdinal("GDTC"));
				if (!reader.IsDBNull(reader.GetOrdinal("CongNhanTN"))) entity.CongNhanTN = reader.GetString(reader.GetOrdinal("CongNhanTN"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertTotNghiepCD(DateTime kyluat, double gDQP, double tTHCM, double monTNCS, double monTNCN, double dTNCTK, double tLDVHPThiLai, string xLTN, string hoTenLot, string ten, string lop, double gDTC, string congNhanTN, string ghiChu)
		{
			TotNghiepCD entity = new TotNghiepCD();	
			entity.Kyluat = kyluat;
			entity.GDQP = gDQP;
			entity.TTHCM = tTHCM;
			entity.MonTNCS = monTNCS;
			entity.MonTNCN = monTNCN;
			entity.DTNCTK = dTNCTK;
			entity.TLDVHPThiLai = tLDVHPThiLai;
			entity.XLTN = xLTN;
			entity.HoTenLot = hoTenLot;
			entity.Ten = ten;
			entity.Lop = lop;
			entity.GDTC = gDTC;
			entity.CongNhanTN = congNhanTN;
			entity.GhiChu = ghiChu;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@Kyluat", SqlDbType.DateTime, Kyluat.Year <= 1753 ? DBNull.Value : (object) Kyluat);
			db.AddInParameter(dbCommand, "@GDQP", SqlDbType.Float, GDQP);
			db.AddInParameter(dbCommand, "@TTHCM", SqlDbType.Float, TTHCM);
			db.AddInParameter(dbCommand, "@MonTNCS", SqlDbType.Float, MonTNCS);
			db.AddInParameter(dbCommand, "@MonTNCN", SqlDbType.Float, MonTNCN);
			db.AddInParameter(dbCommand, "@DTNCTK", SqlDbType.Float, DTNCTK);
			db.AddInParameter(dbCommand, "@TLDVHPThiLai", SqlDbType.Float, TLDVHPThiLai);
			db.AddInParameter(dbCommand, "@XLTN", SqlDbType.NVarChar, XLTN);
			db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
			db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@GDTC", SqlDbType.Float, GDTC);
			db.AddInParameter(dbCommand, "@CongNhanTN", SqlDbType.NVarChar, CongNhanTN);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<TotNghiepCD> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepCD item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateTotNghiepCD(string maSV, DateTime kyluat, double gDQP, double tTHCM, double monTNCS, double monTNCN, double dTNCTK, double tLDVHPThiLai, string xLTN, string hoTenLot, string ten, string lop, double gDTC, string congNhanTN, string ghiChu)
		{
			TotNghiepCD entity = new TotNghiepCD();			
			entity.MaSV = maSV;
			entity.Kyluat = kyluat;
			entity.GDQP = gDQP;
			entity.TTHCM = tTHCM;
			entity.MonTNCS = monTNCS;
			entity.MonTNCN = monTNCN;
			entity.DTNCTK = dTNCTK;
			entity.TLDVHPThiLai = tLDVHPThiLai;
			entity.XLTN = xLTN;
			entity.HoTenLot = hoTenLot;
			entity.Ten = ten;
			entity.Lop = lop;
			entity.GDTC = gDTC;
			entity.CongNhanTN = congNhanTN;
			entity.GhiChu = ghiChu;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_CDTM_TCD_TotNghiepCD_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@Kyluat", SqlDbType.DateTime, Kyluat.Year == 1753 ? DBNull.Value : (object) Kyluat);
			db.AddInParameter(dbCommand, "@GDQP", SqlDbType.Float, GDQP);
			db.AddInParameter(dbCommand, "@TTHCM", SqlDbType.Float, TTHCM);
			db.AddInParameter(dbCommand, "@MonTNCS", SqlDbType.Float, MonTNCS);
			db.AddInParameter(dbCommand, "@MonTNCN", SqlDbType.Float, MonTNCN);
			db.AddInParameter(dbCommand, "@DTNCTK", SqlDbType.Float, DTNCTK);
			db.AddInParameter(dbCommand, "@TLDVHPThiLai", SqlDbType.Float, TLDVHPThiLai);
			db.AddInParameter(dbCommand, "@XLTN", SqlDbType.NVarChar, XLTN);
			db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
			db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@GDTC", SqlDbType.Float, GDTC);
			db.AddInParameter(dbCommand, "@CongNhanTN", SqlDbType.NVarChar, CongNhanTN);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<TotNghiepCD> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepCD item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateTotNghiepCD(string maSV, DateTime kyluat, double gDQP, double tTHCM, double monTNCS, double monTNCN, double dTNCTK, double tLDVHPThiLai, string xLTN, string hoTenLot, string ten, string lop, double gDTC, string congNhanTN, string ghiChu)
		{
			TotNghiepCD entity = new TotNghiepCD();			
			entity.MaSV = maSV;
			entity.Kyluat = kyluat;
			entity.GDQP = gDQP;
			entity.TTHCM = tTHCM;
			entity.MonTNCS = monTNCS;
			entity.MonTNCN = monTNCN;
			entity.DTNCTK = dTNCTK;
			entity.TLDVHPThiLai = tLDVHPThiLai;
			entity.XLTN = xLTN;
			entity.HoTenLot = hoTenLot;
			entity.Ten = ten;
			entity.Lop = lop;
			entity.GDTC = gDTC;
			entity.CongNhanTN = congNhanTN;
			entity.GhiChu = ghiChu;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			db.AddInParameter(dbCommand, "@Kyluat", SqlDbType.DateTime, Kyluat.Year == 1753 ? DBNull.Value : (object) Kyluat);
			db.AddInParameter(dbCommand, "@GDQP", SqlDbType.Float, GDQP);
			db.AddInParameter(dbCommand, "@TTHCM", SqlDbType.Float, TTHCM);
			db.AddInParameter(dbCommand, "@MonTNCS", SqlDbType.Float, MonTNCS);
			db.AddInParameter(dbCommand, "@MonTNCN", SqlDbType.Float, MonTNCN);
			db.AddInParameter(dbCommand, "@DTNCTK", SqlDbType.Float, DTNCTK);
			db.AddInParameter(dbCommand, "@TLDVHPThiLai", SqlDbType.Float, TLDVHPThiLai);
			db.AddInParameter(dbCommand, "@XLTN", SqlDbType.NVarChar, XLTN);
			db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
			db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@GDTC", SqlDbType.Float, GDTC);
			db.AddInParameter(dbCommand, "@CongNhanTN", SqlDbType.NVarChar, CongNhanTN);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<TotNghiepCD> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepCD item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteTotNghiepCD(string maSV)
		{
			TotNghiepCD entity = new TotNghiepCD();
			entity.MaSV = maSV;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_TCD_TotNghiepCD_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.NVarChar, MaSV);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<TotNghiepCD> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (TotNghiepCD item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}